/**
 * 
 */
package util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;

/**
 * @author 10045706
 * 
 */
public class FileConfigReader {

	public enum MonitorModeEnum {
		NOTDEFINED, // not defined
		SECURITY, // only in security mode
		FIRE_SECURITY
		// security and fire mode
	}

	private final static String PARAMETER_NAME = "name";
	private final static String PARAMETER_DESC = "desc";
	private final static String PARAMETER_RETRYINTERVAL = "retry_interval";
	private final static String PARAMETER_TTL = "ttl";
	private final static String PARAMETER_KEEPALIVEREPORTTIME = "keep_alive_report_time";
	private final static String PARAMETER_MONITORMODE = "monitor_mode";
	private final static String PARAMETER_WARNINGTIME = "warning_time";
	private final static String PARAMETER_ERRORTIME = "error_time";

	private final static String MONITOR_MODE_SECURITY = "security";
	private final static String MONITOR_MODE_SECURITY_FIRE = "security_fire";

	StringBuffer sb = new StringBuffer();
	private String name = "DEFAULT";
	private String description = "DEFAULT";
	private MonitorModeEnum monitorMode = MonitorModeEnum.NOTDEFINED;
	private int retryInterval = Integer.MIN_VALUE;
	private int ttl = Integer.MIN_VALUE;
	private int keepAliveReportTime = Integer.MIN_VALUE;
	private int warningTime = Integer.MIN_VALUE;
	private int errorTime = Integer.MIN_VALUE;

	public FileConfigReader(String filepath) throws FileReadingException,
			InvalidFileFormatException {
		try {
			BufferedReader inputFile = new BufferedReader(new FileReader(
					filepath));
			String str;
			while ((str = inputFile.readLine()) != null) {
				sb.append(str);
				sb.append("\n");
			}
			inputFile.close();
			str = sb.toString();
			if ((str == null) || (str.length() <= 0)) {
				throw new FileReadingException("File seems to be empty");
			}
			this.parseConfigFile(str);
		} catch (Exception e) {
			throw new FileReadingException(e.getMessage());
		}
	}

	private void parseConfigFile(String configs)
			throws InvalidFileFormatException {
		String tempId;
		String tempValue;

		StringTokenizer st = new StringTokenizer(configs, "\n");
		while (st.hasMoreTokens()) {
			StringTokenizer config = new StringTokenizer(st.nextToken(), "=");
			tempId = config.nextToken();
			if (config.hasMoreTokens()) {
				tempValue = config.nextToken();
			} else {
				throw new InvalidFileFormatException("Field " + tempId
						+ " does not have value assigned!");
			}
			if (tempId.equalsIgnoreCase(PARAMETER_NAME)) {
				this.name = tempValue;
			} else if (tempId.equalsIgnoreCase(PARAMETER_DESC)) {
				this.description = tempValue;
			} else if (tempId.equalsIgnoreCase(PARAMETER_RETRYINTERVAL)) {
				try {
					this.retryInterval = Integer.parseInt(tempValue);
				} catch (Exception e) {
					throw new InvalidFileFormatException("Field " + tempId
							+ " does not have a valid value assigned!");
				}
			} else if (tempId.equalsIgnoreCase(PARAMETER_TTL)) {
				try {
					this.ttl = Integer.parseInt(tempValue);
				} catch (Exception e) {
					throw new InvalidFileFormatException("Field " + tempId
							+ " does not have a valid value assigned!");
				}
			} else if (tempId.equalsIgnoreCase(PARAMETER_KEEPALIVEREPORTTIME)) {
				try {
					this.keepAliveReportTime = Integer.parseInt(tempValue);
				} catch (Exception e) {
					throw new InvalidFileFormatException("Field " + tempId
							+ " does not have a valid value assigned!");
				}
			} else if (tempId.equalsIgnoreCase(PARAMETER_MONITORMODE)) {
				try {
					if (tempValue
							.equalsIgnoreCase(FileConfigReader.MONITOR_MODE_SECURITY)) {
						this.monitorMode = MonitorModeEnum.SECURITY;
					} else if (tempValue
							.equalsIgnoreCase(FileConfigReader.MONITOR_MODE_SECURITY_FIRE)) {
						this.monitorMode = MonitorModeEnum.FIRE_SECURITY;
					}
				} catch (Exception e) {
					throw new InvalidFileFormatException("Field " + tempId
							+ " does not have a valid value assigned!");
				}
			} else if (tempId.equalsIgnoreCase(PARAMETER_WARNINGTIME)) {
				try {
					this.warningTime = Integer.parseInt(tempValue);
				} catch (Exception e) {
					throw new InvalidFileFormatException("Field " + tempId
							+ " does not have a valid value assigned!");
				}
			} else if (tempId.equalsIgnoreCase(PARAMETER_ERRORTIME)) {
				try {
					this.errorTime = Integer.parseInt(tempValue);
				} catch (Exception e) {
					throw new InvalidFileFormatException("Field " + tempId
							+ " does not have a valid value assigned!");
				}
			}
		}
	}
	
	
	public int getWarningTime() {
		return warningTime;
	}

	public int getErrorTime() {
		return errorTime;
	}

	public String getName() {
		return name;
	}

	public String getDescription() {
		return description;
	}

	public int getRetryInterval() {
		return retryInterval;
	}

	public int getTtl() {
		return ttl;
	}

	public int getKeepAliveReportTime() {
		return keepAliveReportTime;
	}

	public MonitorModeEnum getMonitorMode() {
		return monitorMode;
	}

	/*
	 * public static void main(String[] args) { try { FileConfigReader
	 * confReader = new FileConfigReader("D:/temp.txt");
	 * System.out.println(confReader.getName());
	 * System.out.println(confReader.getDescription());
	 * System.out.println(confReader.getRetryInterval());
	 * System.out.println(confReader.getTtl());
	 * System.out.println(confReader.getKeepAliveReportTime()); } catch
	 * (FileReadingException e) { // TODO Auto-generated catch block
	 * e.printStackTrace(); } catch (InvalidFileFormatException e) { // TODO
	 * Auto-generated catch block e.printStackTrace(); }
	 * 
	 * }
	 */

}
